package com.tsinghua.server.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsinghua.server.base.entity.FxIndustyDebt;
import com.tsinghua.server.base.entity.vo.IndustryVo;
import com.tsinghua.server.base.entity.vo.ProvinceDataVo;
import com.tsinghua.server.base.mapper.FxIndustyDebtMapper;
import com.tsinghua.server.base.service.EChartDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @description:
 * @author: Mr.Eric
 * @create: 2022/2/24 21:20
 */

@Service
@Slf4j
@RequiredArgsConstructor
public class EChartDataServiceImpl extends ServiceImpl<FxIndustyDebtMapper, FxIndustyDebt> implements EChartDataService {

    private final FxIndustyDebtMapper mapper;

    @Override
    public Map<String, List<Integer>> industryDebt() {
        // 获取 2018-1月 到 2022年1月 每个月的最后一天
        List<String> dateList = new ArrayList<>();
        for (int j = 2018; j <= 2022; j++) {
            for (int i = 1; i <= 12; i++) {
                if (j == 2022 && i == 2) {
                    break;
                }
                dateList.add(getLastDayOfMonth1(j, i));
            }
        }


        List<Integer> breach = new ArrayList<>();
        List<Integer> notBreach = new ArrayList<>();
        // 处理违约的数据
        dateList.forEach(obj -> {
            breach.add(mapper.isBreach(obj));
            notBreach.add(mapper.notBreach(obj));
        });


        Map<String, List<Integer>> result = new HashMap<>();
        result.put("breach", breach);
        result.put("notBreach", notBreach);

        return result;
    }

    @Override
    public Map<String,Object> data2() {
        Map<String,Object> result = new HashMap<>();

        List<IndustryVo> industryVos = mapper.countIndustry();
        LinkedList<String> industryName = new LinkedList<>();
        LinkedList<String> industryNum = new LinkedList<>();
        industryVos.forEach(obj->{
            industryName.add(obj.getName());
            industryNum.add(obj.getValue().toString());
        });


        result.put("industryName", industryName);
        result.put("industryNum", industryNum);
        result.put("industryVos",industryVos);



        List<ProvinceDataVo> provinceDataVos = mapper.provinceLr();
        LinkedList<String> provinceName = new LinkedList<>();
        LinkedList<String> jlr = new LinkedList<>();

        provinceDataVos.forEach(obj->{
            if (StringUtils.isNotBlank(obj.getSf())){
                provinceName.add(obj.getSf());
                jlr.add(obj.getLr());
            }

        });

        result.put("provinceName", provinceName);
        result.put("jlr", jlr);
        return result;
    }


    public static String getLastDayOfMonth1(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }


    public static void main(String[] args) {
        for (int j = 2018; j <= 2022; j++) {
            for (int i = 1; i <= 12; i++) {
                if (j == 2022 && i == 2) {
                    break;
                }
                System.out.println(getLastDayOfMonth1(j, i));
            }
        }
    }


}
